package com.gluck.cases.common.utils.utils;

import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.exception.BizSerException;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * AssertUtil
 *
 * @author shiyongzhe
 * @date 10/21/21
 * @desc 断言工具类
 **/
public class AssertUtil {

    /**
     * 断言表达式值为true 否则抛出异常
     * @param expValue
     * @param resultCode
     * @param objs
     */
    public static void isTrue(final boolean expValue, BizSerErrorEnum resultCode, final Object... objs){
        if(expValue){
            resultCode = resultCode != null ? resultCode : BizSerErrorEnum.SYSTEM_ERROR;
            String logString = getLogString(objs);
            String resultMsg = StringUtils.isBlank(logString) ? resultCode.getErrorMsg() : logString;
            throw new BizSerException(resultCode, resultMsg);
        }
    }

    /**
     * 断言表达式值为false 否则抛出异常
     * @param expValue
     * @param resultCode
     * @param objs
     */
    public static void isFalse(final boolean expValue, final BizSerErrorEnum resultCode, final Object... objs){
        isTrue(!expValue, resultCode, objs);
    }

    /**
     * 判断对象是否为null 否则抛出异常
     * @param target
     * @param resultCode
     * @param objs
     */
    public static void isNull(final Object target, final BizSerErrorEnum resultCode, final Object... objs ){
        isTrue(target == null, resultCode, objs);
    }

    /**
     * 判断对象是否不为null 否则抛出异常
     * @param target
     * @param resultCode
     * @param objs
     */
    public static void isNotNull(final Object target, final BizSerErrorEnum resultCode, final Object... objs){
        isTrue(target != null, resultCode, objs);
    }

    /**
     * 判断两个字符串忽略大小写之后相等 否则抛出异常
     * @param obj1
     * @param obj2
     * @param resultCode
     * @param objs
     */
    public static void equalsIgnoreCase(final String obj1, final String obj2, final BizSerErrorEnum resultCode, final Object... objs){
        isTrue(obj1 == null ? obj2 == null : StringUtils.endsWithIgnoreCase(obj1, obj2), resultCode, objs);
    }

    /**
     * 判断两个对象不相等 否则抛出异常
     * @param obj1
     * @param obj2
     * @param resultCode
     * @param objs
     */
    public static void notEquals(final Object obj1, final Object obj2, final BizSerErrorEnum resultCode, final Object... objs){
        isTrue(obj1 == null ? obj2 != null : !obj1.equals(obj2), resultCode, objs);
    }

    /**
     * 判断集合是否为空 否则抛出异常
     * @param collection
     * @param resultCode
     * @param objs
     */
    public static void isEmpty(final Collection collection, final BizSerErrorEnum resultCode, final Object... objs){
        isTrue(isEmpty(collection), resultCode, objs);
    }

    /**
     * 判断集合是否为空
     * @param collection
     * @return
     */
    public static boolean isEmpty(Collection collection){
        return collection == null || collection.isEmpty();
    }

    /**
     * 判断字符串是否为空 否则抛出异常
     * @param str
     * @param resultCode
     * @param objs
     */
    public static void bland(final String str, final BizSerErrorEnum resultCode, final Object... objs){
        isTrue(StringUtils.isBlank(str), resultCode, objs);
    }

    /**
     * 判断字符串不为空 否则抛出异常
     * @param str
     * @param resultCode
     * @param objs
     */
    public static void notBlank(final String str, final BizSerErrorEnum resultCode, final Object... objs){
        isTrue(StringUtils.isNotBlank(str), resultCode, objs);
    }

    /**
     * 判断对象是否相等 否则抛出异常
     * @param base
     * @param target
     * @param resultCode
     * @param objs
     */
    public static void is(final Object base, final Object target, final BizSerErrorEnum resultCode, final Object... objs){
        isTrue(base == target, resultCode, objs);
    }

    /**
     * 生成输出日志字符串，调用格式如下 getLogString("userId={0},username={1}","1","shiyongzhe88")
     * @param objs
     * @return
     */
    public static String getLogString(Object... objs){
        if(objs == null || objs.length == 0){
            return null;
        }
        List<Object> desList = new LinkedList<>();
        boolean isFirst = true;
        for (Object obj : objs){
            if(isFirst){
                isFirst = false;
                continue;
            }
            desList.add(obj);
        }
        return MessageUtil.formatMessage((String) objs[0], desList.toArray());
    }
}
